package cn.bs.busuo.account.service.impl;

import cn.bs.busuo.account.dao.persist.repository.ISightTypeRepository;
import cn.bs.busuo.account.pojo.entity.SightType;
import cn.bs.busuo.account.pojo.param.Type;
import cn.bs.busuo.account.pojo.param.TypeState;
import cn.bs.busuo.account.pojo.param.addType;
import cn.bs.busuo.account.pojo.vo.TypeStateVO;
import cn.bs.busuo.account.pojo.vo.TypeVO;
import cn.bs.busuo.account.service.ISightTypeService;
import cn.bs.busuo.common.enumerator.ServiceCode;
import cn.bs.busuo.common.ex.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class SightTypeServiceImpl implements ISightTypeService {
    @Autowired
    private ISightTypeRepository sightTypeRepository;

    //新增票数
    @Override
    public void addType(Type type) {
//        判断是否有该新增门票的门票类型
        TypeVO typeVO = sightTypeRepository.selectType(type);
        if (typeVO==null){
            String message = "新增失败，没有找到给类型门票！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        SightType sightType = new SightType();
        BeanUtils.copyProperties(type, sightType);
//        新增门票
        sightTypeRepository.addType(sightType);
    }

    //减少票数
    @Override
    public void redType(Type type) {
        TypeVO typeVO = sightTypeRepository.selectType(type);
        if (typeVO==null){
            String message = "减少失败，没有找到给类型门票！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        /**获取库存票数*/
        Long usable = typeVO.getUsable();
        /**判断是否小于库存*/
        if (type.getUsable() >= usable) {
            String message = "减少失败，减少的票数必须小于等于剩余票数！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        SightType sightType = new SightType();
        BeanUtils.copyProperties(type, sightType);
//        减少门票
        sightTypeRepository.redType(sightType);
    }

    //新增票型
    @Override
    public void insertType(addType addType) {
        Type type=new Type();
        BeanUtils.copyProperties(addType,type);
        TypeVO typeVO = sightTypeRepository.selectType(type);
        if (typeVO != null) {
            String message = "添加失败，该景点已存在此类型门票！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        SightType sightType = new SightType();
        BeanUtils.copyProperties(addType, sightType);
        sightType.setTotal(sightType.getUsable());
        sightTypeRepository.insertType(sightType);
    }

    //删除票型
    @Override
    public void delectType(Long id) {
        sightTypeRepository.delectType(id);
    }

    //改变审核状态
    @Override
    public void updateState(TypeState typeState) {

        TypeStateVO typeStateVO = sightTypeRepository.selectTypeState(typeState);
        Integer state = typeStateVO.getCheckState();
        if (state != 0) {
            String message = "重复操作,该数据已被审核!!!!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        Integer checkState = typeState.getCheckState();
        if (checkState != 1 && checkState != 2) {
            String message = "审核失败,你的审核状态码有误!!!!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        sightTypeRepository.updateState(typeState);
    }
}
